package com.tianqu.tms.www.proxy.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;

public class CarProxySearchResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(CarProxySearchResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferPersonalCarInfo person = new TransferPersonalCarInfo();
			String basicCarId = (String) uc.getAttribute("basicCarId");
			String serial = (String) uc.getAttribute("serial");
			uc.removeAttribute("basicCarId");
			uc.removeAttribute("serial");
			TmsUserProfile profile = uc.getUserProfile();
			person = person.getPersonalCarInfoBySerial(basicCarId);
			
			Car car =new Car();
			car = car.searchAllCarByLicensePlate(person.getLicensePlate());
			if(car!=null)
			{
//				if(car.getRegisterStatus() == 1 || car.getRegisterStatus() == 2)
				if(car.getRegisterStatus() != 3 && car.getRegisterStatus() != 4)//modify by huangwei
				{
					//更新
					//# 车辆过户状态（status）“为“ 5：过户中（信息补充）”
				}else{
					car = setCarFromEntity(car, person);
					Sequence seq = Sequence.getInstance();
					String newSerial = seq.getSerial(1);
					car.setSerial(newSerial);
					car.setInsertUserId(profile.getUniqueId());
					car.setUpdateUserId(profile.getUniqueId());
					car.setStatus(person.getStatus());
					car.setAttestStatus(person.getAttestStatus());
					car.setRegisterStatus(1);
					car.setEntryStatus(4);
					car.setCarScore(0);// 楼层
					car.insertPersonalCarInfo(car);

					// 更新过户信息表
					TransferAcceptedInfo accept = new TransferAcceptedInfo();
					accept.setMarketType(1);
					accept.setBasicCarId(newSerial);
					accept.setSerial(serial);
					accept.setUpdateUserId(profile.getUniqueId());
					accept.updateMarketTypeBySerial(accept);
					person.deletePersonalInfoBySerial(basicCarId);						
				}
			}else{
				car = setCarFromEntity(car, person);
				Sequence seq = Sequence.getInstance();
				String newSerial = seq.getSerial(1);
				car.setSerial(newSerial);
				car.setInsertUserId(profile.getUniqueId());
				car.setUpdateUserId(profile.getUniqueId());
				car.setStatus(person.getStatus());
				car.setAttestStatus(person.getAttestStatus());
				car.setRegisterStatus(1);
				car.setEntryStatus(4);
				car.setCarScore(0);// 楼层
				car.insertPersonalCarInfo(car);

				// 更新过户信息表
				TransferAcceptedInfo accept = new TransferAcceptedInfo();
				accept.setMarketType(1);
				accept.setBasicCarId(newSerial);
				accept.setSerial(serial);
				accept.setUpdateUserId(profile.getUniqueId());
				accept.updateMarketTypeBySerial(accept);
				person.deletePersonalInfoBySerial(basicCarId);	
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

	private Car setCarFromEntity(Car car, TransferPersonalCarInfo person) {
		car.setAttribute(person.getAttribute());
		car.setBrandId(person.getBrandId());
		car.setCardType(person.getCardType());
		car.setLicensePlate(person.getLicensePlate());
		car.setCarOwner(person.getCarOwner());
		car.setCarOwnerIdCardNo(person.getCarOwnerIdCardNo());
		car.setCarType(person.getCarType());
		car.setCarUse(person.getCarUse());
		car.setColor(person.getColor());
		car.setCompanyContactPerson(person.getCompanyContactPerson());
		car.setCompanyContactPhone(person.getCompanyContactPhone());
		car.setCountryStyle(person.getCountryStyle());
		car.setDisplacement(person.getDisplacement());
		car.setEngine(person.getEngine());
		car.setFuelType(person.getFuelType());
		car.setGearbox(person.getGearbox());
		car.setJqx(person.getJqx());
		car.setLoadquality(person.getLoadquality());
		car.setCarStyleId(person.getCarStyleId());
		car.setMileage(person.getMileage());
		car.setModelCode(person.getModelCode());
		car.setOwnerAddress(person.getOwnerAddress());
		car.setOwnerOrgNo(person.getOwnerOrgNo());
		car.setOwnerPhone(person.getOwnerPhone());
		car.setOwnerType(person.getOwnerType());
		car.setPassenger(person.getPassenger());
		car.setPrice(person.getPrice());
		car.setCarName(person.getCarName());
		car.setVendorId(person.getVendorId());
		car.setRegDate(person.getRegDate());
		car.setRegisterCardNo(person.getRegisterCardNo());
		car.setSendCardDate(person.getSendCardDate());
		car.setSerial(person.getSerial());
		car.setPhoto_1(person.getPhoto_1());
		car.setPhoto_2(person.getPhoto_2());
		car.setPhoto_3(person.getPhoto_3());
		car.setPhoto_4(person.getPhoto_4());
		car.setPhoto_5(person.getPhoto_5());
		car.setPhoto_6(person.getPhoto_6());
		car.setPhoto_7(person.getPhoto_7());
		car.setPhoto_8(person.getPhoto_8());
		car.setVendorCode(person.getVendorCode());
		car.setVin(person.getVin());
		car.setYearCarefulPeriod(person.getYearCarefulPeriod());
		return car;
	}
}
